package whu.cn.whushare.ui.main.my;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.LoadingHelper;
import whu.cn.whushare.ui.common.MyRelationReShowFragment;
import whu.cn.whushare.ui.main.activity.ActivityDetailActivity;
import whu.cn.whushare.ui.main.activity.bean.Activity;
import whu.cn.whushare.ui.main.known.QuestionDetailActivity;
import whu.cn.whushare.ui.main.known.bean.Question;
import whu.cn.whushare.ui.main.my.adapter.MyActivityListAdapter;
import whu.cn.whushare.ui.main.my.adapter.MyAnswerListAdapter;
import whu.cn.whushare.ui.main.my.adapter.MyQuestionListAdapter;
import whu.cn.whushare.ui.main.my.bean.MyActivity;
import whu.cn.whushare.ui.main.my.bean.MyQuestion;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2014/12/27.
 */
public class RelationActivityFragment extends MyRelationReShowFragment {
    @ViewMapping(id = R.id.activity_list)
    private PullToRefreshListView mRefreshListView;
    private MyActivityListAdapter adapter;
    private LoadingHelper loadingHelper;
    private boolean mHasInited;
    private MyActivity myActivity;

    private String mActivityId;
    private int mTotal=20000;
    private int mCurPage = 1;
    private final static int PAGE_SIZE = 5;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initParams();
        View root = inflater.inflate(R.layout.fragment_relation_activity
                , container, false);//container 为父控件
        initViews(root);
        return root;
    }
    private void initParams() {

    }

    private void requestMyActivityList(int page) {
        Downloader.getInstance(getActivity()).getMyActivityList(page, this, page);
        loadingHelper.showLoadingView(mHasInited);
    }

    private void initViews(View root) {
        ViewMappingUtil.mapView(this, root);
        mRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                requestMyActivityList(1);
                Utils.resetRefreshLabel(getActivity(), mRefreshListView);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                if (mCurPage * PAGE_SIZE >= mTotal) {
                    Utils.setRefreshLabelToLast(getActivity(), mRefreshListView);
                    Utils.COMMON_HANDLER.sendMessageDelayed(
                            Message.obtain(Utils.COMMON_HANDLER, Utils.REFRESH_TO_COMPLETE, mRefreshListView), 500);
                } else {
                    requestMyActivityList(mCurPage + 1);
                }
            }
        });
    }

    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);
        mCurPage = (int) obj;
        switch (type) {
            case MY_ACTIVITY:
                MyActivity activity = (MyActivity) response;
                myActivity=activity;
                adapter = new MyActivityListAdapter(getActivity(),myActivity);
                mRefreshListView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                if(myActivity.Activity.size()<PAGE_SIZE)
                    mTotal=mCurPage*PAGE_SIZE;
                mHasInited = true;
                loadingHelper.showContentView();
                mRefreshListView.onRefreshComplete();
                break;
            default:
                break;
        }
    }

    @Override
    public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
        super.onFail(type, error);
        if (!mHasInited) {
            loadingHelper.showRetryView();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initActions();
        loadingHelper = new LoadingHelper(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestMyActivityList(mCurPage);
            }
        });
        loadingHelper.onCreateView(LayoutInflater.from(getActivity()), mRefreshListView);
        requestMyActivityList(mCurPage);
    }

    private void initActions() {
        mRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Activity activity = (Activity) adapter.getItem(arg2 - 1);
                mActivityId =  String.valueOf(activity.getActivityId());
                startActivityForResult(ActivityDetailActivity.getStartIntent(getActivity(), mActivityId), 1);
            }

        });

    }
}


